package com.hs.sfzy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.HeartTest;
import com.hs.sfzy.service.HeartTestService;
import com.hs.sfzy.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HeartTestController
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年10月26日 15:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/hearttest")
public class HeartTestController {

    @Autowired
    HeartTestService heartTestService;

    @GetMapping
    public Object getAllHeartTest() {
        return ResultUtil.success().setData(heartTestService.getAllHeartTest());
    }

    /**
     * 分页查询试题
     * @author QinKunPeng
     * @date 2024/10/26 21:19
     * @param pageNum
     * @param pageSize
     * @return java.lang.Object
     */
    @GetMapping("/page")
    public Object getHearTestByPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HeartTest> heartTestList = heartTestService.getAllHeartTest();
        PageInfo<HeartTest> pageInfo = new PageInfo<>(heartTestList);
        return ResultUtil.success().setData(pageInfo);
    }

    /**
     * 根据测评id查询试题
     * @author QinKunPeng
     * @date 2024/10/26 21:19
     * @param heartId
     * @return java.lang.Object
     */
    @GetMapping("/byheartid")
    public Object getTestByHeartId(@RequestParam(value = "heartId", required = false) Integer heartId) {
        if (heartId == null) {
            return ResultUtil.fail("数据异常");
        }
        List<HeartTest> testByHeartId = heartTestService.getTestByHeartId(heartId);
        return ResultUtil.success().setData(testByHeartId);
    }

    /**
     * 根据测评id批量查询试题
     * @author QinKunPeng
     * @date 2024/10/26 21:19
     * @param heartIds
     * @return java.lang.Object
     */
    @GetMapping("byheartids")
    public Object getTestByHeartIds(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(value = "heartIds", required = false) Integer[] heartIds) {
        if (heartIds == null) {
            return ResultUtil.fail("数据异常");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<HeartTest> testByHeartIds = heartTestService.getTestByHeartIds(heartIds);
        PageInfo<HeartTest> pageInfo = new PageInfo<>(testByHeartIds);
        return ResultUtil.success().setData(pageInfo);
    }

    /**
     * 根据id删除试题
     * @author QinKunPeng
     * @date 2024/10/26 21:18
     * @param id
     * @return java.lang.Object
     */
    @DeleteMapping("/{id}")
    public Object deleteById(@PathVariable Integer id) {
        return ResultUtil.success().setData(heartTestService.deleteById(id));
    }

    /**
     * 新增试题
     * @author QinKunPeng
     * @date 2024/10/26 21:18
     * @param heartTest
     * @return java.lang.Object
     */
    @PostMapping
    public Object insert(@RequestBody HeartTest heartTest) {
        if (heartTest == null) {
            return ResultUtil.fail("数据异常");
        }
        return ResultUtil.success().setData(heartTestService.insert(heartTest));
    }

    /**
     * 更新试题信息
     * @author QinKunPeng
     * @date 2024/10/26 22:47
     * @param heartTest
     * @return java.lang.Object
     */
    @PutMapping
    public Object update(@RequestBody HeartTest heartTest){
        if (heartTest == null) {
            return ResultUtil.fail("数据异常");
        }else{
            int num = heartTestService.updateHeartTest(heartTest);
            if (num > 0) {
                return ResultUtil.success();
            }else{
                return ResultUtil.fail("更新失败");
            }
        }

    }




}
